package com.wy2.eclipse.tool.ui.dialog;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Dictionary;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wy2.eclipse.tool.ui.UIActivator;
import com.wy2.eclipse.tool.ui.PluginUIImages;
import com.wy2.eclipse.tool.ui.resource.Messages;

public class AboutDialog extends Dialog {
    
    private static final Logger logger=LoggerFactory.getLogger(AboutDialog.class);
    
    private static final int Width=500;
    private static final int Height=450;

    /**
     * Create the dialog.
     * @param parentShell
     */
    public AboutDialog(Shell parentShell) {
        super(parentShell);
    }
    
    @Override
    protected void configureShell(Shell newShell) {
        newShell.setText(Messages.getString("wy2.dialog.about.title"));
        newShell.setImage(PluginUIImages.ABOUT_DIALOG_ICON_TITLE);
        super.configureShell(newShell);
    }

    /**
     * Create contents of the dialog.
     * @param parent
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        
        RowLayout rLayout = new RowLayout(SWT.VERTICAL);
        rLayout.marginWidth=10;
        rLayout.marginHeight=10;
        container.setLayout(rLayout);
        
        Group group = new Group(container, SWT.NONE);
        group.setLayoutData(new RowData(Width-40, 140));
        group.setText("插件信息");
        //com_Base_1=new Composite(container,SWT.NONE);
        //com_Base.setLayoutData(new RowData(Width-10, 200));
        GridLayout groupLayout = new GridLayout(3, false);
        groupLayout.horizontalSpacing=10;
        groupLayout.verticalSpacing=10;
        groupLayout.marginWidth=20;
        groupLayout.marginHeight=10;
        group.setLayout(groupLayout);
        
        
        Bundle bundle = UIActivator.getDefault().getBundle();
        String pluginId=bundle.getSymbolicName();
        Dictionary<String, String> bundleHeads = bundle.getHeaders();
        String pluginVendor=bundleHeads.get("Bundle-Vendor");
        String pluginName=bundleHeads.get("Bundle-Name");
        String pluginVersion=bundleHeads.get("Bundle-Version");
        
        
        createLabelRow(group, "pluginId", pluginId);
        createLabelRow(group, "pluginVendor", pluginVendor);
        createLabelRow(group, "pluginName", pluginName);
        createLabelRow(group, "pluginVersion", pluginVersion);

        
        Composite com_Info=new Composite(container,SWT.NONE);
        com_Info.setLayoutData(new RowData(Width-30, Height-290));
        com_Info.setLayout(new FillLayout(SWT.HORIZONTAL));
        
        Text info=new Text(com_Info, SWT.NONE | SWT.WRAP | SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL);
        info.setText(Messages.getString("wy2.dialog.about.remark"));

        return container;
    }
    
    private void createLabelRow(Composite com_Base,String labelName,String labelValue) {
        
        
        Label label = new Label(com_Base, SWT.RIGHT);
        label.setText(labelName);
        
        Label valueLabel = new Label(com_Base, SWT.NONE);
        valueLabel.setText(labelValue);
        valueLabel.setLayoutData(generateLeftGridData(2,1));
    }
    
    private GridData generateLeftGridData(int horizontalSpan, int verticalSpan) {
        return new GridData(SWT.FILL, SWT.CENTER, true, false, horizontalSpan,verticalSpan);
    }

    /**
     * Create contents of the button bar.
     * @param parent
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OPEN_ID, Messages.getString("wy2.dialog.about.opensite"), false);
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    }
    
    @Override
    protected void buttonPressed(int buttonId) {
        if (buttonId == IDialogConstants.OK_ID) {
            this.setReturnCode(Window.CANCEL);
            this.close();
        }else if(buttonId==IDialogConstants.OPEN_ID) {
            try {
                URI uri = new URI(Messages.getString("wy2.dialog.about.site"));
                Desktop.getDesktop().browse(uri);
                this.setReturnCode(Window.OK);
                this.close();
            } catch (URISyntaxException e) {
                logger.error("", e);
            }catch (IOException e) {
                logger.error("", e);
            }
        }
        super.buttonPressed(buttonId);
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(Width, Height);
    }

}
